import { useUserStore } from '@/store'

// 权限判断指令
const permission = (el, binding) => {
    const { userInfo } = useUserStore()
    const userRoles = userInfo.roles || []
    const needRoles = binding.value || []
    // 求2个数组的交集
    const haveRoles = userRoles.filter(role => needRoles.includes(role))
    // 判断需要的权限和交集数组的长度是否一致,如果一致说明有权限,否则没有权限
    if(needRoles.length !== haveRoles.length){
        el.remove?.()
    }
}

// 不是管理员就不显示元素
const admin = (el, binding) => {
    const { userType } = useUserStore()
    if(userType !== '1'){
        el.remove?.()
    }
}

export default {
    install(app) {
        app.directive('permission', permission)
        app.directive('admin', admin)
    }
}